package com.apestech.framework.sql.modul;

import java.util.ArrayList;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;

public class WhereSql {
	
	/*"SAL_XSDD" :[]
	"SAL_XSDDMX" :[]
	"ROLE":{"SAL_XSDD" :[]
			"SAL_XSDDMX" :[]
	        }
	*/
	
	private JSONObject sqlmap=new JSONObject();
	
	public void add(String tabName, String fieldName, String sql, boolean role, Object value) throws Exception{
		if(!role){
			sqlmap.put(tabName, getFieldMap(tabName, fieldName, sql, value));
		} else {
			sqlmap.put("ROLE", getRoleMap(tabName, fieldName, sql, value));
		}
	}

	public void add(WhereSql param) throws Exception{
		add(param, -1);
	}

	public void add(WhereSql param, int index) throws Exception{
		int len=param.size();
		ArrayList<String> klist=param.getKeyList();
		for(int i=0; i<len; i++){
			String key=klist.get(i);
			if("ROLE".equals(key)){
				JSONObject rmap=param.getRoleCondition();
				int rlen=rmap.size();
				ArrayList<String> rlist=rmap.getKeylist();
				for(int r=0; r<rlen; r++){
					String rkey=rlist.get(r);
					JSONObject role=this.sqlmap.getJSONObject("ROLE");
					JSONArray dlist=null;
					if(role!=null){
						dlist=role.getJSONArray(rkey);
					} else {
						this.sqlmap.put("ROLE", new JSONObject());
					}
					JSONArray slist=param.getRoleCondition(rkey);
					this.sqlmap.getJSONObject("ROLE").put(rkey,insJSONArray(slist, dlist));
				}
			} else {
				JSONArray dlist=this.sqlmap.getJSONArray(key);
				JSONArray slist=param.getTableCondition(key);
				this.sqlmap.put(key,insJSONArray(slist, dlist));
			}
		}
	}

	private JSONArray insJSONArray(JSONArray slist, JSONArray dlist){
		JSONArray nlist;
		if(slist!=null){
			int len=slist.size();
			if(dlist==null){
				dlist=new JSONArray();
			}
			for(int i=0; i<len; i++){
				dlist.add(slist.get(i));
			}
		}
		nlist=dlist;
		return nlist;
	}
	
	public ArrayList<String> getKeyList(){
		return this.sqlmap.getKeylist();
	}
	
	public int size(){
		return this.sqlmap.size();
	}
	
	private JSONArray getFieldMap(String tabName, String fieldName, String sql, Object value){
		JSONArray list=(JSONArray) sqlmap.get(tabName);
		if(list==null){
			list=new JSONArray();
		}
		list.add(getFieldMap(fieldName, sql, value));
		return list;
	}
	
	private JSONObject getFieldMap(String fieldName, String sql, Object value){
		JSONObject map=new JSONObject();
		map.put("field", fieldName);
		map.put("sql", sql);
		map.put("value", value);
		return map;
	}
	
	private JSONObject getRoleMap(String tabName, String fieldName, String sql, Object value) throws Exception{
		JSONObject map=null;
		if(sqlmap.get("ROLE")!=null){
			map=sqlmap.getJSONObject("ROLE").getJSONObject(tabName);
		}
		if(map==null){
			map=new JSONObject();
		}
		JSONArray list=map.getJSONArray(tabName);
		if(list==null){
			list=new JSONArray();
		}
		list.add(getFieldMap(fieldName, sql, value));
		map.put(tabName, list);
		return map;
	}
	
	public JSONArray getTableCondition(String tabName) throws Exception{
		return sqlmap.getJSONArray(tabName);
	}
	
	public JSONObject getRoleCondition() throws Exception{
		return sqlmap.getJSONObject("ROLE");
	}
	
	public JSONArray getRoleCondition(String tabName) throws Exception{
		if(getRoleCondition()==null) return null;
		return sqlmap.getJSONObject("ROLE").getJSONArray(tabName);
	}
	
	public ArrayList<String> getRoleKey() throws Exception{
		JSONObject map=sqlmap.getJSONObject("ROLE");
		if(map==null) return null;
		return map.getKeylist();
	}
	
	public String toString(){
		return sqlmap.toString();
	}

}
